//
//  SettingController.m
//  collection
//
//  Created by zhangjing on 12/21/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "SettingController.h"

@interface SettingController()

@property (retain, nonatomic) IBOutlet UITableView *contentTableView;
@property (nonatomic, strong)   NSArray*    tableArray;

- (IBAction)onClickBackBarItem:(id)sender;
- (void)onSwitchMessageTip:(id)sender;
- (void)onSwitchAutoSavePhoto:(id)sender;
- (void)loadSetting;

@end

@implementation SettingController

@synthesize contentTableView = _contentTableView;
@synthesize tableArray = _tableArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
        NSArray* section1 = [NSArray arrayWithObjects:@"新消息提醒", @"照片自动存储本地相册", nil];
        NSArray* section2 = [NSArray arrayWithObjects:@"高", @"中", @"低", nil];
        self.tableArray = [NSArray arrayWithObjects:section1, section2, nil];
    }
    
    return self;
}

- (void)dealloc {
    self.tableArray = nil;
    [_contentTableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView {
    [super loadView];
    
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.contentTableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"all_bg"]];
}

- (void)viewDidUnload
{
    [self setContentTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.tableArray = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return [self.tableArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [[self.tableArray objectAtIndex:section] count];
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return @"   图片上传质量";
    }
    else {
        return nil;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* cellID = [NSString stringWithFormat:@"Section%d_Row%d", indexPath.section, indexPath.row];
    
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
        cell.indentationLevel = 1;
        cell.indentationWidth = 15.0f;
        cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0f];
        cell.textLabel.frame = CGRectMake(25, 5, 100, 34);
        cell.textLabel.text = [[self.tableArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        if (indexPath.section == 0) {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UISwitch* cellSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
            cell.accessoryView = cellSwitch;
            if (indexPath.row == 0) {
                [cellSwitch addTarget:self action:@selector(onSwitchMessageTip:) forControlEvents:UIControlEventValueChanged];
            }
            else if (indexPath.row == 1) {
                [cellSwitch addTarget:self action:@selector(onSwitchAutoSavePhoto:) forControlEvents:UIControlEventValueChanged];
            }
            [cellSwitch release];
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    return cell;
}

#pragma mark - Impelementation of UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return;
    }
    else if (indexPath.section == 1) {
        for (int i = 0; i < [[self.tableArray objectAtIndex:indexPath.section] count]; i++) {
            NSIndexPath* path = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            UITableViewCell* cell = [tableView cellForRowAtIndexPath:path];
            if (i == indexPath.row) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Private methods

- (IBAction)onClickBackBarItem:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadSetting {
    
}

- (void)onSwitchMessageTip:(id)sender {
    
}

- (void)onSwitchAutoSavePhoto:(id)sender {
    
}

@end
